//
//  ForgetPasswordView.swift
//  SCA
//
//  Created by tjun on 2025/9/2.
//

import UIKit
import TTTAttributedLabel


class ForgetPasswordView: UIView {
 
    let usernameTextField = LabeledTextField(tip: "用户名 至少6个字符，必须由字母和数字所组合。".localizedString)
    let passwordTextField = LabeledTextField(tip: "密码 至少8个字符，必须由字母,数字和特殊字符组合。".localizedString)
    let comfirmPasswordTextField = LabeledTextField(tip: "请重新输入您的密码。".localizedString)
    let otpTextField = LabeledTextField()
    var onConfirmButtonTaped: (() -> Void)?
    private let confirmButton = MainButton(type: .system, title: "确认".localizedString, font: UIFontMake(16), textColor: .white)

    var lastView: UIView!

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
  
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        
        let titleView = UIView().addTo(self)
        titleView.snp.makeConstraints { make in
            make.top.equalTo(0)
            make.left.right.equalToSuperview().inset(AppStyle.margin.left)
            make.height.equalTo(50)
        }
        
        let titleLabel = UILabel(text: "忘记密码".localizedString, font: UIFontMake(16), textColor: .white)
        titleView.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.equalTo(0)
        }
        
        let titleLineView = UIView().addTo(titleView)
        titleLineView.backgroundColor = UIColorMake(0x8692B9)
        titleLineView.snp.makeConstraints { make in
            make.bottom.equalTo(0)
            make.left.right.equalToSuperview()
            make.height.equalTo(1)
        }
        
        usernameTextField.titleAttributedText = "用户名*".localizedString.attributedString(rangeString: "*", rangeColor: UIColorMake(0xFF0000))
        usernameTextField.textField.keyboardType = .asciiCapable
        usernameTextField.textField.addTarget(self, action: #selector(textFieldDidChange(_ :)), for: .editingChanged)
        addSubview(usernameTextField)
        usernameTextField.snp.makeConstraints { make in
            make.top.equalTo(titleView.snp.bottom).offset(20)
            make.left.right.equalTo(titleView)
        }
        
        passwordTextField.titleAttributedText = "密码*".localizedString.attributedString(rangeString: "*", rangeColor: UIColorMake(0xFF0000))
        passwordTextField.textField.keyboardType = .asciiCapable
        addSubview(passwordTextField)
        passwordTextField.textField.addTarget(self, action: #selector(textFieldDidChange(_ :)), for: .editingChanged)
        passwordTextField.snp.makeConstraints { make in
            make.top.equalTo(usernameTextField.snp.bottom).offset(20)
            make.left.right.equalTo(usernameTextField)
        }

        comfirmPasswordTextField.titleAttributedText = "确认密码*".localizedString.attributedString(rangeString: "*", rangeColor: UIColorMake(0xFF0000))
        comfirmPasswordTextField.textField.keyboardType = .asciiCapable
        comfirmPasswordTextField.textField.addTarget(self, action: #selector(textFieldDidChange(_ :)), for: .editingChanged)
        addSubview(comfirmPasswordTextField)
        comfirmPasswordTextField.snp.makeConstraints { make in
            make.top.equalTo(passwordTextField.snp.bottom).offset(20)
            make.left.right.equalTo(usernameTextField)
        }
        
        otpTextField.titleAttributedText = "2FA OTP*".attributedString(rangeString: "*", rangeColor: UIColorMake(0xFF0000))
        addSubview(otpTextField)
        otpTextField.textField.addTarget(self, action: #selector(textFieldDidChange(_ :)), for: .editingChanged)
        otpTextField.snp.makeConstraints { make in
            make.top.equalTo(comfirmPasswordTextField.snp.bottom).offset(20)
            make.left.right.equalTo(usernameTextField)
        }
        
        confirmButton.addTo(self)
        confirmButton.isEnabled = false
        confirmButton.addTarget(self, action: #selector(confirmButtonTapped), for: .touchUpInside)
        confirmButton.snp.makeConstraints { make in
            make.left.right.equalTo(usernameTextField)
            make.height.equalTo(AppStyle.size.buttonHeight.scaled)
            make.top.equalTo(otpTextField.snp.bottom).offset(16)
        }
        
        lastView = confirmButton
    }
    
    func reset() {
        usernameTextField.textField.text = nil
        passwordTextField.textField.text = nil
        comfirmPasswordTextField.textField.text = nil
        otpTextField.textField.text = nil
        textFieldDidChange(usernameTextField.textField)
    }
    
    @objc func textFieldDidChange(_ sender: UITextField) {
        if let username = usernameTextField.textField.text,
           let password = passwordTextField.textField.text,
           let comfirmPassword = comfirmPasswordTextField.textField.text,
           let otp = otpTextField.textField.text,
           isValidUsername(username),
           isValidPassword(password),
           password == comfirmPassword,
           otp.count > 0
        {
            confirmButton.isEnabled = true
        } else {
            confirmButton.isEnabled = false
        }
    }
    
    @objc func confirmButtonTapped() {
        onConfirmButtonTaped?()
    }
}
 
